﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KZ.Exception;
using KZ.Lottery.Entities;
using KZ.Lottery.Resources;
using KZ.Utilities;

namespace KZ.Lottery.DAL
{
    public class RuleReportDAL
    {
        private static RuleReport CreateRuleReportFromReader(SqlDataReader reader)
        {
            RuleReport ruleReport = new RuleReport
            {
                Date = Utility.DateTimeParse(reader[RuleReportColumns.Date.ToString()]),
                RuleID = Utility.StringParse(reader[RuleReportColumns.RuleID.ToString()]),
                RuleName = Utility.StringParse(reader[RuleReportColumns.RuleName.ToString()]),
                ChannelID = Utility.StringParse(reader[RuleReportColumns.ChannelID.ToString()]),
                ChannelName = Utility.StringParse(reader[RuleReportColumns.ChannelName.ToString()]),
                HeadCapital = Utility.DecimalParse(reader[RuleReportColumns.HeadCapital.ToString()]),
                TailCapital = Utility.DecimalParse(reader[RuleReportColumns.TailCapital.ToString()]),
                HeadActualCapital = Utility.DecimalParse(reader[RuleReportColumns.HeadActualCapital.ToString()]),
                TailActualCapital = Utility.DecimalParse(reader[RuleReportColumns.TailActualCapital.ToString()]),
                HeadWinAmount = Utility.DecimalParse(reader[RuleReportColumns.HeadWinAmount.ToString()]),
                TailWinAmount = Utility.DecimalParse(reader[RuleReportColumns.TailWinAmount.ToString()]),
            };

            ruleReport.SetCapital();
            ruleReport.SetActualCapital();
            ruleReport.SetWinAmount();
            ruleReport.SetRevenue();

            return ruleReport;
        }

        public static decimal GetRuleReportForRevenue(DateTime? date)
        {
            try
            {
                List<RuleReport> ruleReportList = new List<RuleReport>();

                DataAccessHelper.SqlCmd.Parameters.Add("Date", SqlDbType.Date).Value = date;
                DataAccessHelper.SqlCmd.Parameters.Add("Revenue", SqlDbType.NVarChar, 50);

                DataAccessHelper.SqlCmd.Parameters["Revenue"].Direction = ParameterDirection.Output;

                DataAccessHelper.ExecuteNonQueryNotClearCommand(CommandType.StoredProcedure, KZ_StoreName.RuleReportGetForRevenue);

                decimal revenue = Utility.DecimalParse(DataAccessHelper.SqlCmd.Parameters["Revenue"].Value);

                DataAccessHelper.ClearCommand();

                return revenue;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetRuleReportForRevenue", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEREPORT_GETLIST);
            }
        }

        public static List<RuleReport> GetRuleReportListForRule(string ruleID, bool fromStart, DateTime from, DateTime to)
        {
            try
            {
                List<RuleReport> ruleReportList = new List<RuleReport>();

                DataAccessHelper.SqlCmd.Parameters.Add("RuleID", SqlDbType.NVarChar).Value = ruleID;
                DataAccessHelper.SqlCmd.Parameters.Add("FromStart", SqlDbType.Bit).Value = fromStart;
                DataAccessHelper.SqlCmd.Parameters.Add("From", SqlDbType.Date).Value = from;
                DataAccessHelper.SqlCmd.Parameters.Add("To", SqlDbType.Date).Value = to;
                using (SqlDataReader reader = DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.RuleReportGetListForRule))
                {
                    while (reader.Read())
                    {
                        ruleReportList.Add(CreateRuleReportFromReader(reader));
                    }
                }

                return ruleReportList;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetRuleReportListForRule", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEREPORT_GETLIST);
            }
        }

        public static List<RuleReport> GetRuleReportListForChannel(string ruleID, bool fromStart, DateTime from, DateTime to)
        {
            try
            {
                List<RuleReport> ruleReportList = new List<RuleReport>();

                DataAccessHelper.SqlCmd.Parameters.Add("RuleID", SqlDbType.NVarChar).Value = ruleID;
                DataAccessHelper.SqlCmd.Parameters.Add("FromStart", SqlDbType.Bit).Value = fromStart;
                DataAccessHelper.SqlCmd.Parameters.Add("From", SqlDbType.Date).Value = from;
                DataAccessHelper.SqlCmd.Parameters.Add("To", SqlDbType.Date).Value = to;
                using (SqlDataReader reader = DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.RuleReportGetListForChannel))
                {
                    while (reader.Read())
                    {
                        ruleReportList.Add(CreateRuleReportFromReader(reader));
                    }
                }

                return ruleReportList;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetRuleReportListForChannel", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEREPORT_GETLIST);
            }
        }

        public bool InsertRuleReport(RuleReport ruleReport)
        {
            try
            {
                DataAccessHelper.SqlCmd.Parameters.Add("Date", SqlDbType.Date).Value = ruleReport.Date;
                DataAccessHelper.SqlCmd.Parameters.Add("RuleID", SqlDbType.NVarChar).Value = ruleReport.RuleID;
                DataAccessHelper.SqlCmd.Parameters.Add("ChannelID", SqlDbType.NVarChar).Value = ruleReport.ChannelID;
                DataAccessHelper.SqlCmd.Parameters.Add("HeadCapital", SqlDbType.Decimal).Value = ruleReport.HeadCapital;
                DataAccessHelper.SqlCmd.Parameters.Add("TailCapital", SqlDbType.Decimal).Value = ruleReport.TailCapital;
                DataAccessHelper.SqlCmd.Parameters.Add("HeadActualCapital", SqlDbType.Decimal).Value = ruleReport.HeadActualCapital;
                DataAccessHelper.SqlCmd.Parameters.Add("TailActualCapital", SqlDbType.Decimal).Value = ruleReport.TailActualCapital;
                DataAccessHelper.SqlCmd.Parameters.Add("HeadWinAmount", SqlDbType.Decimal).Value = ruleReport.HeadWinAmount;
                DataAccessHelper.SqlCmd.Parameters.Add("TailWinAmount", SqlDbType.Decimal).Value = ruleReport.TailWinAmount;
                DataAccessHelper.ExecuteNonQueryWithTransaction(CommandType.StoredProcedure, KZ_StoreName.RuleReportInsert);
                return true;
            }
            catch (SqlException ex)
            {
                Log.LogEx("InsertRuleReport", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEREPORT_INSERT);
            }
        }
    }
}